package com.spring.gop.proj.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.SessionAttributes;

import com.spring.gop.proj.interfaces.MovieService;
import com.spring.gop.proj.interfaces.NoticeService;
import com.spring.gop.proj.interfaces.QnaService;
import com.spring.gop.proj.interfaces.UserInfoService;

import com.spring.gop.proj.model.Buy;
import com.spring.gop.proj.model.MyPoint;
import com.spring.gop.proj.model.Qna;
import com.spring.gop.proj.model.User;

@Controller
@RequestMapping("/myprimus/*")
@SessionAttributes("user")
public class MyPrimusController {

	@Autowired private NoticeService noticeService;
	@Autowired private UserInfoService userinfoService;
	@Autowired private MovieService movieService;
	@Autowired private QnaService qnaService;
	
	@RequestMapping(value="/reservation/reservation")
	public void reservation(HttpSession session, Model model) {
		User newUser = (User)session.getAttribute("user");
		List<Buy> buyList = movieService.selectBuyListMovieForId(newUser.getMember_id());
		session.setAttribute("buyList", buyList);
		List<Qna> qnaList = qnaService.selectListForId(newUser.getMember_id());
		session.setAttribute("qnaList", qnaList);
	}
	
	@RequestMapping(value="/reservation/campsite/buyticket")
	public void buyticket() {
	}
	@RequestMapping(value="/reservation/campsite/myinquiry")
	public void myinquiry() {
	}
	
	@RequestMapping(value="/reservation/campsite/inquirypop")
	public void inquirypop(HttpSession session, Model model, int q_num) {
		User newUser = (User)session.getAttribute("user");
		Map<String, Object> map = new HashMap<String, Object>();
		
		System.out.println(newUser.getMember_id());
		System.out.println(q_num);
		
		map.put("member_id", newUser.getMember_id());
		map.put("q_num", q_num);
		
		System.out.println(userinfoService.selectQnaForId(map));
		model.addAttribute("qna", userinfoService.selectQnaForId(map));
	}
	
	@RequestMapping(value="/mypoint/campsite/mypointgeneralsavelist")
	public void savelist() { }
	@RequestMapping(value="/mypoint/campsite/mypointuselist")
	public void uselist() { }
	
	@RequestMapping(value="/mypoint/mypoint")
	public void mypoint(HttpSession session, Model model) {
		User newUser = (User)session.getAttribute("user");
		List<MyPoint> chargeList = userinfoService.selectIdMyPoint(newUser.getMember_id(), "포인트 충전");
		List<MyPoint> useList = userinfoService.selectIdMyPoint(newUser.getMember_id(), "영화 구매");
		int totalPoint = 0;
		int currentPoint = newUser.getMember_point();
		for(int i=0; i<chargeList.size(); i++) totalPoint += chargeList.get(i).getPoint_value();
		model.addAttribute("totalPoint", totalPoint);
		model.addAttribute("userPoint", totalPoint - currentPoint);
		session.setAttribute("chargeList", chargeList);
		session.setAttribute("useList", useList);
	}
	
	@RequestMapping(value="/meminfomodileave/userinfolog", method=RequestMethod.GET)
	public void userinfologForm() {
	}
	
	@RequestMapping(value="/meminfomodileave/userinfolog", method=RequestMethod.POST)
	public String userinfolog() {
		return "redirect:userinfomodi";
	}
	
	@RequestMapping(value="/meminfomodileave/userinfomodi", method=RequestMethod.GET)
	public void userinfomodiForm(@ModelAttribute("updateUser")User user,Model model) {		
	}	
	
	@RequestMapping(value="/meminfomodileave/userinfomodi", method=RequestMethod.POST)
	public String userinfomodi(@ModelAttribute("updateUser")User user,Model model) {
		
		String email = user.getEheader() +"@"+ user.getEtail();
		String phone = user.getHeader() +"-"+ user.getCenter() +"-"+ user.getTail();
		
		userinfoService.updateUser(email, phone, user.getMember_id());
			
		return "redirect:userinfomodis";
	}
	
	@RequestMapping(value="/meminfomodileave/userinfomodis" ,method=RequestMethod.GET)
	public void userinfomodisFrom() {}
	
	@RequestMapping(value="/meminfomodileave/userinfomodis" ,method=RequestMethod.POST)
	public String userinfomodis(@ModelAttribute("updateUser")User user,HttpSession session) {
		
		return "redirect:userinfomodis";
	}
	
	@RequestMapping(value="/meminfomodileave/campsite/userpwd" ,method=RequestMethod.GET)
	public void userpwdFrom() {
		
	}
	
	@RequestMapping(value="/meminfomodileave/campsite/userpwd", method=RequestMethod.POST)
	public void userpwmodi(@ModelAttribute("updateUser")User user,Model model, HttpSession session) {
		userinfoService.updatePwd(user.getMember_pw(), user.getMember_id());
		User newUser = (User)session.getAttribute("user");
		newUser.setMember_pw(user.getMember_pw());
		session.setAttribute("user", newUser);
	}

	@RequestMapping(value="/meminfomodileave/userleave")
	public void userleave() {
	}

	@RequestMapping(value="/meminfomodileave/userleaves" ,method=RequestMethod.GET)
	public void userleaves(HttpSession session) {
		User user = (User)session.getAttribute("user");
		userinfoService.delUser(user.getMember_id());
		session.invalidate();
	}
}
